/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.page.menu.inbox;


import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Contact;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.renderer.ContactRenderer;
import edu.mobbuzz.util.string.MenuString;
import edu.mobbuzz.util.string.ModeString;
import java.util.Vector;

public class RecepientPage  extends Menu {
    private Form recepientForm;
    private Container recepientPanel;
    private Label menuTitle;
    private DefaultListModel contactListModel;
    private List contactlist;
    private Vector contactArray;
    private Command backCmd;
    private Command selectCmd;
    private Message message;
    private Vector recepientArr;
    private int mode;
    private int messageType;
    
    public RecepientPage(){

        recepientForm = new Form();
        recepientPanel = new Container();
        menuTitle = new Label(getMenuTitle());
        
        contactListModel = new DefaultListModel(displayContactList());
      

        selectCmd = new Command("Select") {
            public void actionPerformed(ActionEvent evt) {
                if(((Contact)contactlist.getSelectedItem()).getName().equals(MenuString.ENTER_CONTACT_ID)){
                    openRecepientFormPage();
                }else{
                    openRecepientContactPage();
                }
            }
        };

        backCmd = new Command("Back") {
            public void actionPerformed(ActionEvent evt) {
                backToMessagePage();
            }
        };


    }

  
    public Vector getRecepientArr() {
        if(recepientArr==null){
            recepientArr = new Vector();
        }
        return recepientArr;
    }


    public void setRecepientArr(Vector recepientArr) {
        this.recepientArr = recepientArr;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    public int getMessageType() {
        return messageType;
    }


    public void setMessageType(int messageType) {
        this.messageType = messageType;
    }
    
    public void showPage(){
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);
        
        recepientPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        recepientPanel.addComponent(menuTitle);
        recepientPanel.addComponent(createList(contactListModel, List.VERTICAL, new ContactRenderer()));

        recepientForm.setLayout(new BorderLayout());
        recepientForm.addComponent(BorderLayout.CENTER, recepientPanel);
        recepientForm.addCommand(backCmd);
        recepientForm.addCommand(selectCmd);
       


        recepientForm.setCommandListener(getActionlistener());
        recepientForm.setScrollable(false);
        recepientForm.show();
    }

    private List createList(DefaultListModel contactsListModel, int orientation, ListCellRenderer renderer) {
        contactlist = new List(contactsListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };
        contactlist.getStyle().setBgTransparency(0);
        contactlist.getStyle().setBorder(Border.getEmpty());
        contactlist.setListCellRenderer(renderer);
        contactlist.setOrientation(orientation);

        return contactlist;
    }

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);

        if(game == Display.GAME_FIRE){
           if(((Contact)contactlist.getSelectedItem()).getName().equals(MenuString.ENTER_CONTACT_ID)){
                openRecepientFormPage();
           }else{
                openRecepientContactPage();
           }


            
            
        }else if(game == Display.GAME_DOWN){
            if(contactlist.getSelectedIndex()<(contactlist.size()-1)){
               contactlist.setSelectedIndex(contactlist.getSelectedIndex()+1);
            }else{
                contactlist.setSelectedIndex(0);
            }

        }else if(game == Display.GAME_UP){
             if(contactlist.getSelectedIndex()!=0){
                 contactlist.setSelectedIndex(contactlist.getSelectedIndex()-1);
             }else{
                 contactlist.setSelectedIndex(contactlist.size()-1);
             }
        }
    }

    public Vector displayContactList() {

        contactArray = new Vector();
        contactArray.addElement(new Contact(0,MenuString.ENTER_CONTACT_ID,"",2));
        contactArray.addElement(new Contact(0,MenuString.CONTACT_LOOK_UP,"",2));

        return contactArray;
    }  

    public void backToMessagePage(){
        System.out.println("RECEPIENT PAGE MODE" + getMode());
        if(getMode() == ModeString.EDIT){
            MessagePage messagePage = new MessagePage();
            messagePage.setExitCmd(getExitCmd());
            messagePage.setActionlistener(getActionlistener());
            messagePage.setMessage(getMessage());
            messagePage.setMode(getMode());
            messagePage.showPage();
        }else if(getMode() == ModeString.NEW){
            MessageFormPage messageFormPage = new MessageFormPage();
            messageFormPage.setExitCmd(getExitCmd());
            messageFormPage.setActionlistener(getActionlistener());
            messageFormPage.setMessage(getMessage());
            messageFormPage.setMode(getMode());
            messageFormPage.showPage();
        }
        
    }

    public void sendMessage() {

    }

    public void displayContactRecepient() {

    }

    public void openRecepientFormPage() {
        
        RecepientFormPage recepientFormPage = new RecepientFormPage();
        recepientFormPage.setExitCmd(getExitCmd());
        recepientFormPage.setActionlistener(getActionlistener());
        recepientFormPage.setMessage(getMessage());
        recepientFormPage.setMode(getMode());
        recepientFormPage.setRecepientArr(getRecepientArr());
        recepientFormPage.setMessageType(getMessageType());
        recepientFormPage.showPage();
    }

    public void openRecepientContactPage() {
        RecepientContactPage recepientContactPage = new RecepientContactPage();
        recepientContactPage.setExitCmd(getExitCmd());
        recepientContactPage.setActionlistener(getActionlistener());
        recepientContactPage.setMessage(getMessage());
        recepientContactPage.setMode(getMode());
        recepientContactPage.setRecepientArr(getRecepientArr());
        recepientContactPage.setMessageType(getMessageType());
        recepientContactPage.showPage();
    }

    public void selectContact() {

    }

    public String getMenuTitle() {
        return MenuString.SEND_TO;
    }

    

    
    

    
}
